package org.csu.jpetstore.controller;


import org.csu.jpetstore.bo.ItemBO;
import org.csu.jpetstore.common.CommonResponse;
import org.csu.jpetstore.common.ResponseCode;
import org.csu.jpetstore.entity.Category;
import org.csu.jpetstore.entity.Item;
import org.csu.jpetstore.entity.Product;
import org.csu.jpetstore.service.CatalogService;
import org.csu.jpetstore.service.ItemService;
import org.csu.jpetstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CSU_Luoyuhang
 * @date: 2022/4/8 18:18
 */

@Controller
@RequestMapping("/catalog/")
public class CatalogController {


    @Autowired
    private CatalogService catalogService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ItemService itemService;

    @GetMapping("categories")
    @ResponseBody
    public CommonResponse<List<Category>> getCategoryList() {
        List<Category> categoryList = catalogService.getCategoryList();
        if (categoryList.isEmpty()) {
            return CommonResponse.createForSuccessMsg("未查询到该分类信息");
        }
        return CommonResponse.createForSuccess(categoryList);
    }

    @GetMapping("categories/{id}")
    @ResponseBody
    public CommonResponse<Category> getCategory(@PathVariable("id") String categoryId) {
        Category category = catalogService.getCategory(categoryId);
        if (category == null) {
            return CommonResponse.createForSuccessMsg("未查到该Category");
        }
        return CommonResponse.createForSuccess(category);
    }

    @GetMapping("products/{id}")
    @ResponseBody
    public CommonResponse<Product> getProduct(@PathVariable("id") String productId) {
        Product product = productService.getProduct(productId);
        if (product == null) {
            return CommonResponse.createForSuccessMsg("未查询到该ProductID下的商品信息");
        }
        return CommonResponse.createForSuccess(product);
    }


    @GetMapping("categories/{id}/products")
    @ResponseBody
    public CommonResponse<List<Product>> getProductList(@PathVariable("id") String categoryId) {
        List<Product> productList = productService.getProductListByCategoryId(categoryId);
        if (productList.isEmpty()) {
            return CommonResponse.createForSuccessMsg("未查询到该categoryID下的商品信息");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @GetMapping("products/keyword/{keyword}")
    @ResponseBody
    public CommonResponse<List<Product>> getProductListByProductId(@PathVariable("keyword") String keyword) {
        List<Product> productList = productService.getProductListByProductIdAndName(keyword);
        if (productList.isEmpty()) {
            return CommonResponse.createForSuccessMsg("未查询到该Keyword下的商品信息");
        }
        return CommonResponse.createForSuccess(productList);
    }


    @PostMapping("newCategory/{id}/{name}/{image}/{description}")
    @ResponseBody
    public CommonResponse<Category> createNewCategory(@PathVariable("id") String categoryId, @PathVariable("name") String name, @PathVariable("image") String image, @PathVariable("description") String description) {
        catalogService.createNewCategory(categoryId, name, image, description);
        Category category = catalogService.getCategory(categoryId);
        if (category == null) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Category添加失败");
        }
        return CommonResponse.createForSuccessMsg("Category添加成功");
    }


    @PostMapping("newProduct/{productId}/{categoryId}/{name}/{image}/{description}")
    @ResponseBody
    public CommonResponse<Product> createNewProduct(@PathVariable("productId") String productId, @PathVariable("categoryId") String categoryId, @PathVariable("name") String name, @PathVariable("image") String image, @PathVariable("description") String description) {
        productService.createNewProduct(productId, categoryId, name, image, description);
        Product product = productService.getProduct(productId);
        if (product == null) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "Product添加失败");
        }
        return CommonResponse.createForSuccessMsg("Product添加成功");
    }

    @GetMapping("products/{id}/items")
    @ResponseBody
    public CommonResponse<List<ItemBO>> getItemListByProductId(@PathVariable("id") String productId) {
        List<ItemBO> itemList = itemService.getItemListByProductId(productId);
        if (itemList.isEmpty()) {
            return CommonResponse.createForSuccessMsg("未查询到该productId下的商品");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    @GetMapping("items/{id}")
    @ResponseBody
    public CommonResponse<Item> getItemByItemId(@PathVariable("id") String itemId) {
        Item item = itemService.getItemByItemId(itemId);
        if (item == null) {
            return CommonResponse.createForSuccessMsg("未查询到该商品");
        }
        return CommonResponse.createForSuccess(item);
    }
}
